import time
from selenium import webdriver

url = 'https://bj.58.com/?utm_source=market&spm=u-2d2yxv86y3v43nkddh1.BDPCPZ_BT'

driver = webdriver.Chrome(executable_path=r'E:\ChromeDriver\chromedriver_win32/chromedriver.exe')

driver.get(url)

print(driver.current_url)
print(driver.current_window_handle)  # 打印当前句柄
print(driver.window_handles)  # 打印句柄

# 获取焦点并点击
el = driver.find_element_by_xpath('/html/body/div[3]/div[1]/div[1]/div/div[1]/div[1]/span[1]/a')
el.click()

# 不添加句柄跳转，那么操作的网页永远是第一个网页
# 获取句柄
step_url = driver.window_handles

# 通过句柄跳转页面
driver.switch_to.window(step_url[-1])

el_list = driver.find_elements_by_xpath('/html/body/div[6]/div[2]/ul/li/div[2]/h2/a')
print(len(el_list))
print(driver.current_url)
print(driver.current_window_handle)  # 打印当前句柄
print(driver.window_handles)  # 打印句柄

time.sleep(5)
driver.quit()


